// pages/mall/redPackage/redPackage.js
import { Api } from "../../../utils/api.js"
import { Http } from '../../../utils/http.js'
import { TimeToDate } from '../../../utils/timeToDate.js'
let http = new Http()
let timeToDate = new TimeToDate()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userLogin: 0,
    userDetail: null,
    code: '',
    coupons: [ ],
    hongbaoId: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      hongbaoId: options.hongbaoId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.getStorage({
      key: 'userToken',
      success(res) {
        getApp().globalData.userToken = res.data
        that.setData({
          userLogin: 1
        })
        if (that.data.hongbaoId > 0) {
          that.receiveRedpackageCoupon()
        }
      },
      fail(res) {
        wx.login({
          success: function (res) {
            that.setData({
              code: res.code
            })
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    return {
      title: '【融生活】恭喜获得优惠券红包，赶快分享领取吧！',
      path: '/pages/mall/redPackage/redPackage?hongbaoId=' + this.data.hongbaoId,
      imageUrl: 'https://media.links2all.com/Ft17-5yRJPmMBJH9uay2laMwGFzm',
      success: (res) => {
        if (res.errMsg == 'shareAppMessage:ok') {
          that.setData({
            showRedPackage: 0
          })
        }
      },
      fail: (res) => {

      }
    }
  },

  /**
   * 自定义方法
   */
  goMallIndex: function () {
    wx.switchTab({
      url: '/pages/mall/index/index',
    })
  },

  getUserInfo: function (e) {
    var errMsg = e.detail.errMsg;
    if (errMsg == "getUserInfo:ok") {
      this.setData({
        userDetail: e.detail
      });
      this.userLogin();
    } else {
      wx.showToast({
        title: '用户登录失败',
        icon: 'none'
      })
    }
  },

  userLogin: function () {
    var that = this
    var userDetail = this.data.userDetail
    var code = this.data.code
    http.sendHttp(Api.user_login, {
      data: {
        code: code,
        userDetail: JSON.stringify(userDetail),
      },
      success: function (res) {
        var userToken = res.data.obj.token
        getApp().globalData.userToken = userToken
        wx.setStorage({
          key: 'userToken',
          data: userToken,
        })
        that.onShow()
      }
    }, 'POST')
  },

  receiveRedpackageCoupon: function () {
    var that = this
    http.sendHttp(Api.receive_redpackage_coupon, {
      data: {
        hongbaoId: this.data.hongbaoId
      },
      success: function (res) {
        var coupons = res.data.obj.coupons
        for (var index in coupons) {
          coupons[index].tmExpire = timeToDate.formatTime(coupons[index].tmExpire / 1000, 'Y-M-D')
        }
        that.setData({
          coupons: coupons
        })
      }
    }, 'POST')
  }
})